﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace RegistroMascotas2
{
    public class MascotaRoleProvider : RoleProvider
    {
        #region Atributos
            private static SqlConnection _conn;
            private static SqlCommand _comm;
            private static SqlDataAdapter _adapter;
            private string _cadenaConexion = ConfigurationManager.ConnectionStrings["CadenaConn"].ConnectionString;
        #endregion

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            DataTable dtTipoUsuarios = new DataTable();
            List<string> arrayTipoUsuarios = new List<string>();

            try
            {
                _conn = new SqlConnection(_cadenaConexion);
                _comm = new SqlCommand();
                _comm.Connection = _conn;

                _comm.CommandType = CommandType.StoredProcedure;
                _comm.CommandText = "sp_TipoUsuarios_TraerTodos";

                _adapter = new SqlDataAdapter();
                _adapter.SelectCommand = _comm;
                _adapter.Fill(dtTipoUsuarios);

                foreach(DataRow tipo in dtTipoUsuarios.Rows){
                    arrayTipoUsuarios.Add(tipo["TipoUsuario"].ToString());
                }
            }
            catch (Exception ex)
            {                
                throw ex;
            }

            return arrayTipoUsuarios.ToArray();
        }

        public override string[] GetRolesForUser(string username)
        {
            //acceder a la base y le devuelvo el tipo de usuario del usuario que le pase.
            DataTable dtTipoUsuarios = new DataTable();
            List<string> arrayTipoUsuarios = new List<string>();

            try
            {
                _conn = new SqlConnection(_cadenaConexion);
                _comm = new SqlCommand();
                _comm.Connection = _conn;

                _comm.CommandType = CommandType.StoredProcedure;
                _comm.CommandText = "sp_TipoUsuarios_TraerPorUsuario";

                _comm.Parameters.Add("@nombreUsuario", SqlDbType.VarChar, 50);
                _comm.Parameters[0].Value=username;

                _adapter = new SqlDataAdapter();
                _adapter.SelectCommand = _comm;
                _adapter.Fill(dtTipoUsuarios);

                foreach(DataRow tipo in dtTipoUsuarios.Rows){
                    arrayTipoUsuarios.Add(tipo["TipoUsuario"].ToString());
                }
            }
            catch (Exception ex)
            {                
                throw ex;
            }

            return arrayTipoUsuarios.ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            DataTable dtUsuarios = new DataTable();
            List<string> arrayUsuarios = new List<string>();

            try
            {
                //acceder a la base y me devuelve todos los usuarios que sean de ese tipo de usuario
                _conn = new SqlConnection(_cadenaConexion);
                _comm = new SqlCommand();
                _comm.Connection = _conn;

                _comm.CommandType = CommandType.StoredProcedure;
                _comm.CommandText = "sp_TipoUsuarios_TraerPorTipo";//nombre store

                _comm.Parameters.Add("@nombreUsuario", SqlDbType.VarChar, 50);
                _comm.Parameters[0].Value=roleName;

                _adapter = new SqlDataAdapter();
                _adapter.SelectCommand = _comm;
                _adapter.Fill(dtUsuarios);

                foreach(DataRow user in dtUsuarios.Rows){
                    arrayUsuarios.Add(user["Usuario"].ToString());
                }
            }
            catch (Exception ex)
            {                
                throw ex;
            }

            return arrayUsuarios.ToArray();            
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}
